//
//  BDLangageSettingTableViewController.m
//  BeidouMall
//
//  Created by qie on 16/5/4.
//  Copyright © 2016年 qie. All rights reserved.
//

#import "BDLangageSetTableViewController.h"
#import "LanguageManager.h"
#import "AppDelegate.h"
#import "BDTabBarController.h"
#import "BDHomeViewController.h"
#import "BDSettingTableViewController.h"
#import "BDNavigationController.h"
#import "BDTypeViewController.h"
#import "BDBeidouViewController.h"
#import "BDJianHuoHomeViewController.h"

@interface BDLangageSetTableViewController ()
{
    NSString *temp;
    NSInteger index;
}
@property (weak, nonatomic) IBOutlet UIImageView *chineseImageView;
@property (weak, nonatomic) IBOutlet UIImageView *englishImageView;
@property (weak, nonatomic) IBOutlet UIImageView *tibetanImageView;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;

@end

@implementation BDLangageSetTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"langageSetting", nil);
    
    _chineseImageView.hidden = YES;
    _englishImageView.hidden = YES;
    _tibetanImageView.hidden = YES;
    if ([BDAccountManagerModel shareInstance].langage.length > 0) {
        temp = [BDAccountManagerModel shareInstance].langage;
        if ([[BDAccountManagerModel shareInstance].langage isEqualToString:@"zh"]) {
            _chineseImageView.hidden = NO;
            index = 0;
        }
        if ([[BDAccountManagerModel shareInstance].langage isEqualToString:@"en"]) {
            _englishImageView.hidden = NO;
            index = 1;
        }
        if ([[BDAccountManagerModel shareInstance].langage isEqualToString:@"zang"]) {
            _tibetanImageView.hidden = NO;
            index = 2;
        }
    } else {
        [BDAccountManagerModel shareInstance].langage = @"zh";
        temp = @"zh";
        _chineseImageView.hidden = NO;
        index = 0;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _saveButton.title = NSLocalizedString(@"save", nil);
    if ([[BDAccountManagerModel shareInstance].langage isEqualToString:@"zang"]) {
        [[UIBarButtonItem appearance] setTitleTextAttributes:
         @{NSFontAttributeName:[UIFont fontWithName:@"BZDHT" size:12.0]} forState:UIControlStateNormal];
    } else {
        [[UIBarButtonItem appearance] setTitleTextAttributes:
         @{NSFontAttributeName:[UIFont systemFontOfSize:18.0]} forState:UIControlStateNormal];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private method

- (void)reloadRootViewController {
    dispatch_async(dispatch_get_main_queue(), ^{
        AppDelegate *appDelegate =
        (AppDelegate *)[[UIApplication sharedApplication] delegate];
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        BDHomeViewController *home = [storyBoard instantiateViewControllerWithIdentifier:@"HomeStoryboardID"];
        BDTypeViewController *type = [storyBoard instantiateViewControllerWithIdentifier:@"TypeStoryboardID"];
        BDBeidouViewController *beidou = [storyBoard instantiateViewControllerWithIdentifier:@"BeidouStoryboardID"];
        BDSettingTableViewController *setting = [storyBoard instantiateViewControllerWithIdentifier:@"SettingStoryboardID"];
        BDJianHuoHomeViewController *jianhuo = [storyBoard instantiateViewControllerWithIdentifier:@"JianHuoStoryboardID"];
        UITabBarController *tabVC = (UITabBarController*)appDelegate.window.rootViewController;
        
        tabVC.viewControllers = @[[self addChildVc:home image:@"homeBtn" selectImage:@"homeBtn_s" title:NSLocalizedString(@"home", @"")],
                                  [self addChildVc:jianhuo image:@"JianhuoBtn" selectImage:@"JianhuoBtn_s"  title:NSLocalizedString(@"jianhuo", @"")],
                                  [self addChildVc:type image:@"typeBtn" selectImage:@"typeBtn_s"  title:NSLocalizedString(@"type", @"")],
                                  [self addChildVc:beidou image:@"shopcartBtn" selectImage:@"shopcartBtn_s" title:NSLocalizedString(@"beidou", @"")],
                                  [self addChildVc:setting image:@"myBtn" selectImage:@"myBtn_s"  title:NSLocalizedString(@"my", @"")]];
    });
}

- (BDNavigationController *)addChildVc:(UIViewController *)childVC image:(NSString *)image selectImage:(NSString *)selectImage title:(NSString *)title {
    if ([title isEqualToString:@"背兜"]) {
        childVC.tabBarItem.imageInsets = UIEdgeInsetsMake(0, 2, 0, -2);
    }
    childVC.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.title = title;
    UIFont *font;
    if ([[BDAccountManagerModel shareInstance].langage isEqualToString:@"zang"]) {
        font = [UIFont fontWithName:@"Bzdbt" size:8.0];
    } else {
        font = [UIFont boldSystemFontOfSize:12.0];
    }
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       font,NSFontAttributeName,
                                                       RGBColor(102, 102, 102),NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       RGBColor(109, 38, 25), NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateSelected];
    
    BDNavigationController *nav = [[BDNavigationController alloc] initWithRootViewController:childVC];
    return nav;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        temp = @"zh";
        _chineseImageView.hidden = NO;
        _englishImageView.hidden = YES;
        _tibetanImageView.hidden = YES;
    } else if (indexPath.row == 1) {
        temp = @"en";
        _chineseImageView.hidden = YES;
        _englishImageView.hidden = NO;
        _tibetanImageView.hidden = YES;
    } else if (indexPath.row == 2) {
        temp = @"zang";
        _chineseImageView.hidden = YES;
        _englishImageView.hidden = YES;
        _tibetanImageView.hidden = NO;
    }
    index = indexPath.row;
}

- (IBAction)saveButton:(UIBarButtonItem *)sender {
    [BDAccountManagerModel shareInstance].langage = temp;
    [LanguageManager saveLanguageByIndex:index];
    [self reloadRootViewController];
//    self.block(@"");
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC);
    dispatch_after(time, dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
